﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VipSys.SystemEntity;
using VipSys.DataLayer;

namespace VipSys.Business
{
    public class ViewAuthority
    {
        private static ViewAuthority m_Instance = null;
        /// <summary>
        /// 单实例
        /// </summary>
        public static ViewAuthority Instance
        {
            get
            {
                if (m_Instance == null)
                    m_Instance = new ViewAuthority();
                return m_Instance;
            }
        }
        /// <summary>
        /// 得到当前用户可以看到浏览的学校
        /// </summary>
        /// <returns></returns>
        public string GetSchools(string szRoleType,string szOrgName)
        {
            string schools = string.Empty;
            if (szRoleType == SystemData.RoleType.HEAD)
            {
                return string.Empty;
            }
            Orgnization orgnization = new Orgnization();
            orgnization.OrgName = szOrgName;
            short shRet = OrgnizationAccess.Instance.GetOrgnization(orgnization, ref orgnization);
            Orgnization childOrg = new Orgnization();
            childOrg.ParentID = orgnization.ID.ToString();
            List<string> lstSchools=new List<string>();

            Recursion(childOrg,orgnization.OrgName, ref lstSchools);
            foreach (var item in lstSchools)
            {
                if (schools == string.Empty)
                    schools = string.Format("'{0}'"
                        , item);
                else {
                    schools = string.Format("{0},'{1}'"
                        , schools
                        , item);
                }
            }
            return schools;
        }
        private void Recursion(Orgnization query,string OrgName, ref List<string> lstSchools)
        {
            List<Orgnization> lstOrgnization=new List<Orgnization>();
            short shRet = OrgnizationAccess.Instance.GetOrgnizationList(query, ref lstOrgnization);
            if (lstOrgnization.Count <= 0)
            {
                lstSchools.Add(OrgName);
                return;
            }
            foreach (var item in lstOrgnization)
            {
                Orgnization childOrg = new Orgnization();
                childOrg.ParentID = item.ID.ToString();
                Recursion(childOrg, item.OrgName, ref lstSchools);
            }
        }
    }
}
